package com.fuxue.config;

import com.fuxue.vo.ForumVO;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Configuration
public class RandomConfig {

    public static List<ForumVO> selectRandom(List<ForumVO> originalList) {
        List<ForumVO> selectedList = new ArrayList<>();

        // 创建随机生成器
        Random random = new Random();

        if (originalList.size() < 2) {
            // 如果列表中元素少于两个，直接保存一个元素
            if (!originalList.isEmpty()) {
                selectedList.add(originalList.get(0)); // 选择第一个元素（也可以随机选择）
            }
        } else {
            // 如果列表中元素多于两个，随机选择两个不同的元素
            int firstIndex = random.nextInt(originalList.size());
            int secondIndex;
            do {
                secondIndex = random.nextInt(originalList.size());
            } while (secondIndex == firstIndex);

            selectedList.add(originalList.get(firstIndex));
            selectedList.add(originalList.get(secondIndex));
        }

        return selectedList;
    }
}
